﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace Helix_Network_Supervisor_3
{
    class Playlist
    {
        #region Fields

        private ApplicationSettings appSettings = new ApplicationSettings();
        private FileExplorer fileExplorer = new FileExplorer();
        private TreeNode rootNode = new TreeNode();
        private bool playlistNeedsToBeSaved = false;
        private bool errorInPlaylist = false;
        private int numberOfErrors = 0;
        //bool warningInPlaylist = false;
        private string savePlaylistFilePath = "";
        private FileManipulator fm = new FileManipulator();
        private int numberOfSongsInPlaylist = 0;
        private string currentPlaylistFileName = "New Playlist";
        private int lengthOfPlaylist = 0;
        private long sizeOfPlaylist = 0;
        private int numberOfMP3sAvailable = 0;
        private MP3Header mP3Header = new MP3Header();

        #endregion Fields

        #region Ctor

        #endregion Ctor

        #region Properties

        public bool PlayListNeedsToBeSaved
        {
            get { return playlistNeedsToBeSaved; }
        }

        public string CurrentPlaylistFileName
        {
            get
            {
                return (currentPlaylistFileName);
            }
        }

        public int NumberOfSongsInPlaylist
        {
            get { return (numberOfSongsInPlaylist); }
        }

        public int LengthOfPlaylist
        {
            get { return lengthOfPlaylist; }
        }

        public string FormatedLengtOfPlaylist
        {
            get { return ConvertSecondsToFormatedLength(lengthOfPlaylist); }
        }

        public string FormatedSizeOfPlaylist
        {
            get { return fm.Bytes2MegaBytes(sizeOfPlaylist); }
        }

        public int NumberOfMP3sAvailable
        {
            get { return numberOfMP3sAvailable; }

        }

        #endregion Properties

        #region Public Methods

        /// <summary>
        /// Populate the Playlist List View with the Currently Saved Playlists
        /// </summary>
        /// <param name="playlistListView"></param>
        public void PopulateSavedPlaylistListView(ListView playlistListView)
        {
            playlistListView.Clear();

            playlistListView.Columns.Add("File Name (Double Click to Open)", 200, HorizontalAlignment.Left);
            playlistListView.Columns.Add("File Path", 1, HorizontalAlignment.Left);

            string playlistPath = appSettings.playlistFilePath;

            try
            {
                DirectoryInfo di = new DirectoryInfo(playlistPath);
                FileInfo[] arrFiles = di.GetFiles();

                string[] listItems = new string[2];
                string fileName = "";
                foreach (FileInfo tempFile in arrFiles)
                {
                    fileName = tempFile.Name;
                    fileName.ToUpper();
                    if (!fileName.Equals("PAGEFILE.SYS"))
                    {
                        int iImage = 1;
                        string fileExtension = tempFile.Extension;

                        listItems[0] = fileName;
                        listItems[1] = playlistPath;

                        ListViewItem lviItem = new ListViewItem(listItems, iImage);

                        playlistListView.Items.Add(lviItem);
                    }
                }
            }
            catch
            {
            }

        }

        /// <summary>
        /// Initialize All of the Playlist Controls
        /// </summary>
        /// <param name="playlistDirectoryTreeView"></param>
        /// <param name="playlistAvailableMP3sListView"></param>
        /// <param name="playlistMP3sListView"></param>
        public void InitPlaylistControls(TreeView playlistDirectoryTreeView, ListView playlistAvailableMP3sListView,
            ListView playlistMP3sListView)
        {
            rootNode = fileExplorer.PopulateTreeView(playlistDirectoryTreeView);
            playlistDirectoryTreeView.Parent.Select();

            InitializePlaylistAvailableMP3sListView(playlistAvailableMP3sListView);
            SetupInitialPlaylistAvailableMP3sListView(playlistAvailableMP3sListView);
            InitializePlaylistMP3sListView(playlistMP3sListView);

            numberOfSongsInPlaylist = 0;
            lengthOfPlaylist = 0;
            playlistNeedsToBeSaved = false;
            errorInPlaylist = false;
            savePlaylistFilePath = "";
            currentPlaylistFileName = "New Playlist";
        }

        /// <summary>
        /// Populate the Playlist Directory Tree View Folders
        /// </summary>
        /// <param name="currNode"></param>
        /// <param name="playlistDirectoryTreeView"></param>
        /// <param name="playlistAvailableMP3sListView"></param>
        public void PopulatePlaylistDirectoryTreeViewFolders(TreeNode currNode, TreeView playlistDirectoryTreeView, 
            ListView playlistAvailableMP3sListView)
        {
            //TreeNode currNode = playlistDirectoryTreeView.SelectedNode;
            string currentPath = currNode.FullPath;

            InitializePlaylistAvailableMP3sListView(playlistAvailableMP3sListView);

            if (rootNode == currNode)
            {
                return;
            } // End if

            currentPath = currentPath.Remove(0, 12);

            currNode.Nodes.Clear();

            if (Directory.Exists(currentPath) == false)
            {
                return;
            } // End if
            else
            {
                TreeNodeCollection fileCollection = currNode.Nodes;
                fileExplorer.PopulateFolders(currentPath, fileCollection);
            } // End else
        }

        /// <summary>
        /// Populate the Playlist Available MP3s List View with the MP3s in the Selected Folder
        /// </summary>
        /// <param name="currNode"></param>
        /// <param name="playlistAvailableMP3sListView"></param>
        public void PopulatePlayListAvailableMP3sListView(TreeNode currNode, ListView playlistAvailableMP3sListView)
        {
            //this.Cursor = Cursors.WaitCursor;

            InitializePlaylistAvailableMP3sListView(playlistAvailableMP3sListView);

            if (currNode == rootNode)
            {
                return;
            }

            string currPath = currNode.FullPath;

            currPath = currPath.Remove(0, 12);

            currPath = currPath.Remove(2, 1);

            try
            {
                if (Directory.Exists(currPath) == false)
                {
                    //this.Cursor = Cursors.Arrow;
                    string error1 = "Directory or Path " + currPath + " doesn't exist";
                    MessageBox.Show(error1, "Helix Playlist - Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                } // End if

                appSettings.songPathDirectory = currPath;
                DirectoryInfo di = new DirectoryInfo(currPath);
                FileInfo[] arrFiles = di.GetFiles();

                string[] listItems = new string[2];
                string fileName = "";
                foreach (FileInfo tempFile in arrFiles)
                {
                    fileName = tempFile.Name;
                    fileName.ToUpper();
                    if (!fileName.Equals("PAGEFILE.SYS"))
                    {
                        //int iImage = 1;
                        string fileExtension = tempFile.Extension;

                        fileExtension = fileExtension.ToUpper();
                        if (fileExtension == ".MP3")
                        {
                            listItems[0] = fileName;
                            listItems[1] = (currPath + @"\" + fileName);

                            ListViewItem lviItem = new ListViewItem(listItems, 0);

                            playlistAvailableMP3sListView.Items.Add(lviItem);
                            numberOfMP3sAvailable++;
                        } // End if

                    } // End if
                } // End foreach
                //this.Cursor = Cursors.Arrow;
            } // End try
            catch (IOException e)
            {
                //this.Cursor = Cursors.Arrow;
                string error1 = "Error Occured while accessing directory " + currPath;

                error1 += "\nError Message:  " + e;
                MessageBox.Show(error1, "Helix Playlist - Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Stop);

                //playlistDirectoryTreeView.Parent.Focus();
                return;
            } // End catch (IOException)
            catch (System.UnauthorizedAccessException e)
            {
                MessageBox.Show(e.Message, "Helix Playlist - Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            } // End catch (UnauthorizedAccess Exception)

            //filesStripStatusLabel.Text = "Number of Vixen Files:  " + vixenCount.ToString();
            return;
        } // End PopulatePlayListAvailableMP3sListView()

        /// <summary>
        /// Add the Selected Song to the Playlist at the Specified Insertion Point
        /// </summary>
        /// <param name="playlistMP3sListView"></param>
        /// <param name="selectedFile"></param>
        /// <param name="insertionIndex"></param>
        /// <returns></returns>
        public bool AddSongToPlaylist(ListView playlistMP3sListView, ListViewItem selectedFile, int insertionIndex)
        {
            if (insertionIndex < 0)
            {
                insertionIndex = 0;
            }

            try
            {
                ListViewItem.ListViewSubItemCollection subItems = selectedFile.SubItems;

                string completePath = subItems[1].Text;

                FileInfo fi = new FileInfo(completePath);

                string extension = fi.Extension;
                extension = extension.ToUpper();

                if (extension == ".MP3")
                {
                    mP3Header.ReadMP3Information(completePath);

                    string[] listItems = new string[7];
                    long fileSize = mP3Header.FileSize;
                    string formatedFileSize = fm.Bytes2MegaBytes(fileSize);

                    listItems[0] = Path.GetFileNameWithoutExtension(completePath);
                    listItems[1] = completePath;
                    listItems[2] = mP3Header.FormatedLength; //Formated Length of the MP3
                    listItems[3] = mP3Header.BitRate.ToString() + " Kbps"; //Bit Rate of the MP3
                    listItems[4] = formatedFileSize; //Size of the MP3 in MBytes
                    listItems[5] = fileSize.ToString(); //Size of the MP3 in bytes
                    listItems[6] = mP3Header.Length.ToString(); //Length of the MP3 in seconds

                    ListViewItem lviItem = new ListViewItem(listItems);

                    if (mP3Header.BitRate > 128)
                    {
                        MessageBox.Show("The Helix work best with MP3 encoded at 128 Kbps or less." +
                            "\rThe current file was encoded at " + mP3Header.BitRate.ToString() +
                            " Kbps.  \rThis file may not play correctly.");

                        lviItem.BackColor = Color.Yellow;
                        errorInPlaylist = true;
                        numberOfErrors++;
                    }

                    playlistMP3sListView.Items.Insert(insertionIndex, lviItem);

                        //if drag and drop within the Playlist List View then delete the original Item
                    if (subItems[2].Text == "playlistMP3sListView")
                    {
                        try
                        {
                            playlistMP3sListView.SelectedItems[0].Remove();
                        }
                        catch
                        {

                        }
                    }
                    else
                    {
                        //Adding a new song to the playlist so update length and size
                        sizeOfPlaylist += fileSize;
                        lengthOfPlaylist += mP3Header.Length;
                    }

                        playlistNeedsToBeSaved = true;

                        numberOfSongsInPlaylist = playlistMP3sListView.Items.Count;

                        return true;

                } // End if
                else
                {
                    MessageBox.Show("This is not an MP3 File",
                        "Helix Playlist - Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Stop);

                    return false;
                } // End else
            } // End try
            catch
            {
                MessageBox.Show("No file was selected!", "Helix Playlist - Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Stop);

                return false;
            } // End catch
        } // End AddSongToPlaylist()

        /// <summary>
        /// Delete the Selected Song from the Playlist
        /// </summary>
        /// <param name="playlistMP3sListView"></param>
        /// <returns></returns>
        public bool DeleteSongFromPlaylist(ListView playlistMP3sListView)
        {
            try
            {
                ListView.SelectedListViewItemCollection selectedFile = playlistMP3sListView.SelectedItems;

                ListViewItem.ListViewSubItemCollection subItems = selectedFile[0].SubItems;

                int secs = int.Parse(subItems[6].Text);

                long size = long.Parse(subItems[5].Text);

                lengthOfPlaylist -= secs;
                sizeOfPlaylist -= size;

                ListViewItem selectedItem = selectedFile[0];

                if (selectedItem.BackColor == Color.Yellow)
                {
                    numberOfErrors--;
                    if (numberOfErrors == 0)
                    {
                        errorInPlaylist = false;
                    }
                }

                selectedItem.Remove();
                numberOfSongsInPlaylist = playlistMP3sListView.Items.Count;
                if (numberOfSongsInPlaylist == 0)
                {
                    playlistNeedsToBeSaved = false;
                }
                else
                {
                    playlistNeedsToBeSaved = true;
                }
                return true;

            } // End try
            catch
            {
                MessageBox.Show("No file was selected", "Helix Playlist - Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Stop);

                return false;
            } // End catch
        }

        /// <summary>
        /// Open the Selected Playlist File
        /// </summary>
        /// <param name="playlistMP3sListView"></param>
        /// <param name="openPlaylistFilePath"></param>
        /// <returns></returns>
        public bool OpenPlaylistFile(ListView playlistMP3sListView, string openPlaylistFilePath)
        {
            if (playlistNeedsToBeSaved)
            {
                DialogResult dR = MessageBox.Show("The current playlist has not been saved.  Save the current playlist?",
                    "Helix Playlist Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (dR == DialogResult.Yes)
                {
                    SaveAsPlaylist(playlistMP3sListView);
                }
                else if (dR == DialogResult.Cancel)
                {
                    return false;
                }
            }

            InitializePlaylistMP3sListView(playlistMP3sListView);

            savePlaylistFilePath = openPlaylistFilePath;

            ListView.ListViewItemCollection lvicToOpen = fm.OpenPlaylistFile(savePlaylistFilePath);

            foreach (ListViewItem lvi in lvicToOpen)
            {
                ListViewItem.ListViewSubItemCollection subItems = lvi.SubItems;

                string completePath = subItems[1].Text;

                FileInfo fi = new FileInfo(completePath);

                string extension = fi.Extension;
                extension = extension.ToUpper();

                if (extension == ".MP3")
                {
                    mP3Header.ReadMP3Information(completePath);

                    string[] listItems = new string[7];
                    long fileSize = mP3Header.FileSize;
                    string formatedFileSize = fm.Bytes2MegaBytes(fileSize);

                    listItems[0] = Path.GetFileNameWithoutExtension(completePath);
                    listItems[1] = completePath;
                    listItems[2] = mP3Header.FormatedLength; //Formated Length of the MP3
                    listItems[3] = mP3Header.BitRate.ToString() + " Kbps"; //Bit Rate of the MP3
                    listItems[4] = formatedFileSize; //Size of the MP3 in MBytes
                    listItems[5] = fileSize.ToString(); //Size of the MP3 in bytes
                    listItems[6] = mP3Header.Length.ToString(); //Length of the MP3 in seconds

                    ListViewItem lviItem = new ListViewItem(listItems);

                    if (mP3Header.BitRate > 128)
                    {
                        MessageBox.Show("The Helix work best with MP3 encoded at 128 Kbps or less." +
                            "\rThe current file was encoded at " + mP3Header.BitRate.ToString() +
                            " Kbps.  \rThis file may not play correctly.");

                        lviItem.BackColor = Color.Yellow;
                        errorInPlaylist = true;
                        numberOfErrors++;
                    }

                    sizeOfPlaylist += fileSize;
                    lengthOfPlaylist += mP3Header.Length;

                    playlistMP3sListView.Items.Add(lviItem);
                }
            }

            numberOfSongsInPlaylist = playlistMP3sListView.Items.Count;
            playlistNeedsToBeSaved = false;
            currentPlaylistFileName = Path.GetFileName(savePlaylistFilePath);
            return true;
        }

        /// <summary>
        /// Save the Current Playlist
        /// </summary>
        /// <param name="playlistMP3sListView"></param>
        /// <returns></returns>
        public bool SavePlaylistFile(ListView playlistMP3sListView)
        {
            if (errorInPlaylist == true)
            {
                MessageBox.Show("A playlist can't be saved until all of the MP3 errors are eliminated.", "Helix Playlist - Warning",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return (false);
            }

            if (savePlaylistFilePath == "")
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();

                saveFileDialog.DefaultExt = "ply";
                saveFileDialog.Filter = "Helix Playlist Files|*.ply";

                saveFileDialog.InitialDirectory = appSettings.playlistFilePath;
                //Call the SaveFileDialog
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    savePlaylistFilePath = saveFileDialog.FileName;
                    string fileName = Path.GetFileName(savePlaylistFilePath);
                    savePlaylistFilePath = appSettings.playlistFilePath + @"\" + fileName;
                }
                else
                {
                    return false;
                }
            }

            ListView.ListViewItemCollection lvicToBeSaved = playlistMP3sListView.Items;

            fm.SavePlaylist(lvicToBeSaved, savePlaylistFilePath);
            playlistNeedsToBeSaved = false;
            currentPlaylistFileName = Path.GetFileName(savePlaylistFilePath);
            return true;
        }

        /// <summary>
        /// Save the Current Playlist with a New Name
        /// </summary>
        /// <param name="playlistMP3sListView"></param>
        /// <returns></returns>
        public bool SaveAsPlaylist(ListView playlistMP3sListView)
        {
            if (errorInPlaylist == true)
            {
                MessageBox.Show("A playlist can't be saved until all of the MP3 errors are eliminated.", "Helix Playlist - Warning",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return (false);
            }

            string originalFilePath = savePlaylistFilePath;
            savePlaylistFilePath = "";

            if (SavePlaylistFile(playlistMP3sListView) == true)
            {
                return true;
            }
            else
            {
                savePlaylistFilePath = originalFilePath;
                return false;
            }
        }

        /// <summary>
        /// Create a New Playlist
        /// </summary>
        /// <param name="playlistMP3sListView"></param>
        /// <returns></returns>
        public bool NewPlaylist(ListView playlistMP3sListView)
        {
            if (playlistNeedsToBeSaved)
            {
                DialogResult dR = MessageBox.Show("The current playlist has not been saved.  Save the current playlist?",
                    "Helix Playlist Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (dR == DialogResult.Yes)
                {
                    SavePlaylistFile(playlistMP3sListView);
                }
                else if (dR == DialogResult.Cancel)
                {
                    return false;
                }
            }
            InitializePlaylistMP3sListView(playlistMP3sListView);

            return true;
        }

        /// <summary>
        /// Get the Selected Song from the Playlist Available MP3s List View
        /// </summary>
        /// <param name="playlistAvailableMP3sListView"></param>
        /// <returns></returns>
        public ListViewItem GetSelectedSong(ListView playlistAvailableMP3sListView)
        {
            try
            {
                ListViewItem lviItem = playlistAvailableMP3sListView.SelectedItems[0];

                string[] listItems = new string[4];

                listItems[0] = lviItem.SubItems[0].Text;
                listItems[1] = lviItem.SubItems[1].Text;
                listItems[2] = lviItem.ListView.Name;
                listItems[3] = lviItem.Index.ToString();

                ListViewItem dragItem = new ListViewItem(listItems);

                return dragItem;
            }
            catch
            {
                ListViewItem errorItem = new ListViewItem("error");

                return errorItem;
            }
        }

        /// <summary>
        /// Get the Selected Song from the Playlist MP3 List View
        /// </summary>
        /// <param name="playlistMP3sListView"></param>
        /// <returns></returns>
        public ListViewItem GetSelectedPlaylistItem(ListView playlistMP3sListView)
        {
            try
            {
                ListViewItem lviItem = playlistMP3sListView.SelectedItems[0];

                string[] itemList = new string[4];

                itemList[0] = lviItem.SubItems[0].Text;
                itemList[1] = lviItem.SubItems[1].Text;

                itemList[2] = lviItem.ListView.Name;
                itemList[3] = lviItem.Index.ToString();

                ListViewItem dragItem = new ListViewItem(itemList);

                return dragItem;
            }
            catch
            {
                ListViewItem errorItem = new ListViewItem("error");

                return errorItem;
            }
        }

        #endregion Public Methods

        #region Private Methods

        /// <summary>
        /// Initialize the Playlist MP3s List View
        /// </summary>
        /// <param name="playlistMP3sListView"></param>
        private void InitializePlaylistMP3sListView(ListView playlistMP3sListView)
        {
            playlistMP3sListView.Clear();
            playlistMP3sListView.Columns.Add("Selected Song Titles", 180, HorizontalAlignment.Left);
            playlistMP3sListView.Columns.Add("Path", 0, HorizontalAlignment.Left);
            playlistMP3sListView.Columns.Add("Length", 60, HorizontalAlignment.Left);
            playlistMP3sListView.Columns.Add("Bit Rate", 60, HorizontalAlignment.Left);
            playlistMP3sListView.Columns.Add("Size", 60, HorizontalAlignment.Left);
            playlistMP3sListView.Columns.Add("Size (B)", 0, HorizontalAlignment.Left);
            playlistMP3sListView.Columns.Add("Length (s)", 0, HorizontalAlignment.Left);

            playlistMP3sListView.InsertionMark.Color = Color.Green;

            currentPlaylistFileName = "New Playlist";
            savePlaylistFilePath = "";
            lengthOfPlaylist = 0;
            numberOfSongsInPlaylist = 0;
            errorInPlaylist = false;
        }

        /// <summary>
        /// Initialize the Playlist Available MP3s List View
        /// </summary>
        /// <param name="playlistAvailableMP3sListView"></param>
        private void InitializePlaylistAvailableMP3sListView(ListView playlistAvailableMP3sListView)
        {
            playlistAvailableMP3sListView.Clear();
            playlistAvailableMP3sListView.Columns.Add("Available Song Titles", 195, HorizontalAlignment.Left);
            playlistAvailableMP3sListView.Columns.Add("Path", 0, HorizontalAlignment.Left);
            numberOfMP3sAvailable = 0;
        }

        /// <summary>
        /// Load the Playlist Available MP3s List View with the MP3s in the Last Open Folder
        /// </summary>
        /// <param name="playlistAvailableMP3sListView"></param>
        private void SetupInitialPlaylistAvailableMP3sListView(ListView playlistAvailableMP3sListView)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(appSettings.songPathDirectory);
                FileInfo[] arrFiles = di.GetFiles();

                string[] listItems = new string[2];
                string fileName = "";
                foreach (FileInfo tempFile in arrFiles)
                {
                    fileName = tempFile.Name;
                    fileName.ToUpper();
                    if (!fileName.Equals("PAGEFILE.SYS"))
                    {
                        string fileExtension = tempFile.Extension;

                        fileExtension = fileExtension.ToUpper();

                        if (fileExtension == ".MP3")
                        {
                            listItems[0] = fileName;
                            listItems[1] = appSettings.songPathDirectory + @"\" + fileName;

                            ListViewItem lviItem = new ListViewItem(listItems, 0);

                            playlistAvailableMP3sListView.Items.Add(lviItem);

                            numberOfMP3sAvailable++;
                        } // End if
                    }
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// Convert a String from the format:  MMm SSs to an Integer Number of Seconds
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        private int ConvertFormatedLengthToSeconds(string length)
        {
            try
            {
                string stringMinute = length.Substring(0, 2);
                string stringSecond = length.Substring(3, 2);

                int intMinutes = int.Parse(stringMinute);
                int intSeconds = int.Parse(stringSecond);
                return (intSeconds + 60 * intMinutes);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Convert an Interger Number of Seconds to a String with the Format:  MMm SSs
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        //private string ConvertSecondsToFormatedLength(int seconds)
        //{
        //    int intMinutes = seconds / 60;
        //    int intSeconds = seconds % 60;

        //    string formatedLength;

        //    if (intMinutes < 10)
        //    {
        //        formatedLength = "0" + intMinutes.ToString() + "m ";
        //    }
        //    else
        //    {
        //        formatedLength = (intMinutes.ToString() + "m ");
        //    }

        //    if (intSeconds < 10)
        //    {
        //        formatedLength += "0";
        //    }
        //    formatedLength += (intSeconds.ToString() + "s");

        //    return formatedLength;
        //}

        private string ConvertSecondsToFormatedLength(int s)
        {
            // Seconds to display
            int ss = s % 60;

            // Complete number of minutes
            int m = (s - ss) / 60;

            // Minutes to display
            int mm = m % 60;

            // Complete number of hours
            int h = (m - mm) / 60;

            // Make "hh:mm:ss"

            if (h > 0)
            {
                return h.ToString("D2") + ":" + mm.ToString("D2") + ":" + ss.ToString("D2");
            }
            else
            {
                return mm.ToString("D2") + ":" + ss.ToString("D2");
            }
        }

        #endregion Private Methods
    }
}
